﻿using LazyCache;
using Microsoft.Extensions.Caching.Memory;

namespace Common.Caching;

public class AppCacheProvider : ICacheService
{
    private readonly IAppCache _appCache;

    public AppCacheProvider(IAppCache appCache)
    {
        _appCache = appCache;
    }

    public T Get<T>(string key)
    {
        return _appCache.Get<T>(key);
    }

    public void Set<T>(string key, T value, TimeSpan expiry)
    {
        _appCache.Add(key, value, expiry);
    }

    public void Remove(string key)
    {
        _appCache.Remove(key);
    }

    public async Task<T> GetOrAddAsync<T>(string key, Func<Task<T>> addItemFactory, MemoryCacheEntryOptions? policy = null)
    {
        if (policy == null) 
        {
            return await _appCache.GetOrAddAsync(key, addItemFactory);
        }
        else
        {
            return await _appCache.GetOrAddAsync(key, addItemFactory, policy);
        }
    }
}
